﻿using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace checkFileSize
{
    public delegate void SetProBar(int cur, int all);
    public partial class Form1 : Form
    {
        private SetProBar setProBar;

        public Form1()
        {
            InitializeComponent();
            this.setProBar = this.SetCurPro;
        }

        // .bank 文件对应的文件大小
        private Dictionary<string, int> dicBankToSize = new Dictionary<string, int>();

        // guid 文件对应的 名称
        private Dictionary<string, string> dicGuidToName = new Dictionary<string, string>();

        // file 对应 bankId
        private Dictionary<string, string> dicEventToBankId = new Dictionary<string, string>();

        // bank id 对应的 bank名称 （来之guid文件）和 dicGuidToName 类似了暂不用
        private Dictionary<string, string> dicBankIdToName = new Dictionary<string, string>();

        //// 近期修改的文件  bank
        //private List<string> bankChangeList = new List<string>();

        // 近期修改的文件  event 
        //private List<string> eventChangeList = new List<string>();

        private List<List<string>> list_commonFileChange = new List<List<string>>();

        //snapshot 变化的列表
        private List<List<string>> list_snapshot = new List<List<string>>();
        //private Dictionary<string, string> dicEventToSnapshot = new Dictionary<string, string>();
        // snapshot 变化 涉及其对应的 event 列表 (最后还需要根据event列表 对应到 .bank文件上)
        private Dictionary<string, List<string>> dic_snapShotChange = new Dictionary<string, List<string>>();

        /// <summary>
        /// 获取当前设置的时间
        /// </summary>
        /// <returns></returns>
        private int GetCurSetTime()
        {
            string strTime = this.text_checkTime.Text;
            if (string.IsNullOrEmpty(strTime))
            {
                strTime = "0";
            }
            int time = Int32.Parse(strTime);
            return time;
        }

        /// <summary>
        /// 近期 .bank 文件修改的大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_bankSize_Click(object sender, EventArgs e)
        {
            if (dicBankToSize.Count <= 0)
            {
                this.SetBankFileSize();
            }
            string path = this.text_pathBank.Text;

            this.GetBankChnageList(path, this.GetCurSetTime());

        }



        /// <summary>
        /// event 变化导致 bank变化的大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_eventSize_Click(object sender, EventArgs e)
        {
            string path = this.text_pathEvent.Text;
            if (dicBankToSize.Count <= 0)
            {
                this.SetBankFileSize();
            }
            this.SetEventToBank();
            this.SetGuidToName();
            this.GetEventFileChangeList(path, this.GetCurSetTime());
        

        }

        /// <summary>
        /// 获取目录下所有文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private FileInfo[] GetFileList(string path)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(path);
            FileInfo[] fileInfos = TheFolder.GetFiles();
            return fileInfos;
        }

        /// <summary>
        /// 获取 .bank 路径下 文件的大小
        /// </summary>
        private void SetBankFileSize()
        {
            if (dicBankToSize.Count > 0)
            {
                return;
            }
            // 遍历 bank 路径下的 .bank文件大小
            string bankPath = this.text_pathBank.Text;
            FileInfo[] fileInfos = this.GetFileList(bankPath);
            int all = fileInfos.Length;
            int cur = 0;
            foreach (var file in fileInfos)
            {
                if (file.FullName.Contains(".meta") == false)
                {
                    string path = file.FullName;
                    string[] temp = path.Split('\\');
                    string[] files = temp[temp.Length - 1].Split('.');
                    string bankName = files[0];  // 文件名 

                    System.IO.FileInfo fileInfo = null;
                    try
                    {
                        fileInfo = new System.IO.FileInfo(path);
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                    if (fileInfo != null && fileInfo.Exists)
                    {
                        int size = (int)System.Math.Ceiling(fileInfo.Length / 1024.0);
                        dicBankToSize[bankName] = size;
                      
                    }
                    cur++;
                    this.setProBar(cur,all);
                }
            }
        }


        /// <summary>
        /// guid 对应的名称
        /// </summary>
        private void SetGuidToName()
        {
            //已经执行过就不重复执行了
            if (this.dicBankIdToName.Count > 0 )
            {
                return;
            }
            string path = this.text_pathGUID.Text;

            if (!File.Exists(path))
            {
                return;
            }
            try
            {
                using (var fs = new FileStream(path, FileMode.Open))
                {
                    int fsLen = (int)fs.Length;
                    byte[] txtByte = new byte[fsLen];
                    int r = fs.Read(txtByte, 0, txtByte.Length);
                    string myStr = System.Text.Encoding.UTF8.GetString(txtByte);
                    string[] curRowStr = myStr.Split('\r', '\n');
                    int all = curRowStr.Length;
                    int cur = 0;
                    for (int i = 0; i < curRowStr.Length; i++)
                    {
                        //道理上只要bank 文件   bank:
                        if (string.IsNullOrEmpty(curRowStr[i]) == false)
                        {
                            string[] curList = Regex.Split(curRowStr[i], "\\s+", RegexOptions.IgnoreCase);//  curRowStr[i].Split("\\s+", RegexOptions.IgnoreCase);
                            string curGuid = curList[0].Trim();
                            string name = curList[1].Trim();
                            this.dicGuidToName[curGuid] = name;

                            if (name.Contains("bank:"))
                            {
                                string[] bans = name.Split('/');
                                this.dicBankIdToName[curGuid] = bans[1];
                            }
                        }
                        cur++;
                        this.setProBar(cur, all);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        ///  event 对应的bank id
        /// </summary>
        private void SetEventToBank()
        {
            // 已经执行过的就不重复执行了
            if (this.dicEventToBankId.Count > 0)
            {
                return;
            }
            string eventFilePath = this.text_pathEvent.Text;

            if (Directory.Exists(eventFilePath))
            {
                FileInfo[] fileInfos = this.GetFileList(eventFilePath);
                int all = fileInfos.Length;
                int cur = 0;
                foreach (var file in fileInfos)
                {
                    this.SetSetEventToBankName(file.FullName);
                    cur++;
                    this.setProBar(cur, all);
                }
            }
        }


        private void SetSetEventToBankName(string path)
        {
            if (File.Exists(path))
            {

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);

                XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
                string[] temp = path.Split('\\');
                string[] files = temp[temp.Length - 1].Split('.');
                string keyname = files[0];  // 文件名 

                foreach (XmlElement element in topM)
                {
                    XmlNodeList xmlNodeList = element.ChildNodes;

                    // 获取节点名称
                    string nodeName = this.GetNodeName(element.Attributes);

                    // Event 节点取  name 和 对应的 banks
                    if (nodeName == "Event")
                    {
                        // 取 指向的 banks  event 名称
                        foreach (XmlElement el in xmlNodeList)
                        {
                            XmlAttributeCollection elattr = el.Attributes;
                            XmlNode elNode = elattr.Item(0);
                            string elName = elNode.Value;

                            if (elName == "banks")
                            {
                                // event 文件对应 bank 的guid
                                string bankGuid = el.InnerText;
                                this.dicEventToBankId[keyname] = bankGuid;
                            }
                        }
                    }
                }
            }
        }

        private void SetBankIdToName()
        {

        }

        /// <summary>
        /// 获取节点名称
        /// </summary>
        /// <param name="attributeCollection"></param>
        /// <returns></returns>
        private string GetNodeName(XmlAttributeCollection attributeCollection)
        {
            XmlNode elNode = attributeCollection.Item(0);
            string elName = elNode.Value;
            return elName;
        }


        private void GetBankChnageList(string path, int time)
        {
            var files = this.ChangeFileInTime(path, time);
            int allSize = 0;
            int all = files.Count;
            int cur = 0;
            List<List<string>> datas = new List<List<string>>();
            foreach (var file in files)
            {
                string[] temp = file.Split('\\');
                string[] names = temp[temp.Length - 1].Split('.');
                string bankNema = names[0];  // 文件名 
                if (this.dicBankToSize.ContainsKey(bankNema))
                {
                    allSize += this.dicBankToSize[bankNema];
                    List<string> data = new List<string>();
                    data.Add(bankNema);
                    data.Add(this.dicBankToSize[bankNema] + "KB");
                    datas.Add(data);
                }
                cur++;
                this.setProBar(cur, all);
            }
            allSize = allSize / 1024;   // 资源大小 M
            List<string> dataEnd = new List<string>();
            dataEnd.Add("总数："+ datas.Count);
            dataEnd.Add(allSize + "M");
            datas.Add(dataEnd);
            Console.Write(allSize);


            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "audio(*.xlsx)|*.xlsx";
            saveFileDialog.ShowDialog();
            string savePath = saveFileDialog.FileName;


            ExcelTool excelTool = new ExcelTool(savePath);
  

            List<List<string>> herders = new List<List<string>>();
            List<string> hlist1 = new List<string>();
            hlist1.Add("修改的bank文件");
            hlist1.Add("单个资源大小");
            herders.Add(hlist1);
            excelTool.CreatSheetData(herders, datas,"近期修改的bank");
            excelTool.SaveAllExcel();
        }

        /// <summary>
        /// event文件 变化的列表
        /// </summary>
        private void GetEventFileChangeList(string path, int time)
        {
            var files = this.ChangeFileInTime(path,time);
            List<string> bankList = new List<string>();
            List<List<string>> curChangeEventlist = new List<List<string>>();
            int all = files.Count;
            int cur = 0;
            foreach (var file in files)
            {
                string[] temp = file.Split('\\');
                string[] names = temp[temp.Length - 1].Split('.');
                string eventName = names[0];  // 文件名 
                
                string bankId = this.dicEventToBankId[eventName];
                if (this.dicBankIdToName.ContainsKey(bankId))
                {
                    string bankName = this.dicBankIdToName[bankId];
                    if (bankList.Contains(bankName) == false)
                    {
                        bankList.Add(bankName);
                    }

                    if (this.dicGuidToName.ContainsKey(eventName))
                    {
                        List<string> oneEvent = new List<string>();
                        oneEvent.Add(eventName);
                        oneEvent.Add(this.dicGuidToName[eventName]);
                        curChangeEventlist.Add(oneEvent);
                    }
                }
                cur++;
                this.setProBar(cur, all);
            }
            List<List<string>> outList = new List<List<string>>();
            all = bankList.Count;
            cur = 0;
            double allSize = 0;
            foreach (var name in bankList)
            {
                if (this.dicBankToSize.ContainsKey(name))
                {
                    List<string> oneList = new List<string>();
                    oneList.Add(name);
                    oneList.Add(this.dicBankToSize[name].ToString() + "K");
                    outList.Add(oneList);
                    allSize += this.dicBankToSize[name];
                }
                cur++;
                this.setProBar(cur, all);
            }
            allSize = allSize / 1024; // 大小 M

            List<string> endList = new List<string>();
            endList.Add("文件总数："+ outList.Count);
            endList.Add("文件总大小：" + allSize + "M");
            outList.Add(endList);

            List<List<string>> heards = new List<List<string>>();
            List<string> hlist1 = new List<string>();
            hlist1.Add("即将修改到的bank文件");
            hlist1.Add("文件大小");
            heards.Add(hlist1);

            List<List<string>> heard2s = new List<List<string>>();
            List<string> hlist2 = new List<string>();
            hlist2.Add("修改的event文件");
            hlist2.Add("event名称");
            heard2s.Add(hlist2);

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "audio(*.xlsx)|*.xlsx";
            saveFileDialog.ShowDialog();
            string savePath = saveFileDialog.FileName;


            ExcelTool excelTool = new ExcelTool(savePath);

            excelTool.CreatSheetData(heards, outList,"event修改涉及bank");
            excelTool.CreatSheetData(heard2s, curChangeEventlist, "修改涉及的event");
            excelTool.SaveAllExcel();
        }

        /// <summary>
        /// 时间范围内 路径下有修改的文件路径列表(默认只有一层文件夹) 本地修改时间
        /// </summary>
        /// <param name="path"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        private List<string> ChangeFileInTime(string path,int time)
        {
            var curDataTime = DateTime.Now.Date;
            List<string> list = new List<string>();
            if (Directory.Exists(path))
            {
                FileInfo[] fileInfos = this.GetFileList(path);
                int all = fileInfos.Length;
                int cur = 0;
                foreach (var file in fileInfos)
                {
                    if (this.IsFileInNeedTime(curDataTime, file.LastWriteTime, file,time))
                    {
                        list.Add(file.FullName);
                    }
                    cur++;
                    this.setProBar(cur, all);
                }
            }
            return list;
        }


        /// <summary>
        /// 文件是否在修改的时间内
        /// </summary>
        /// <returns></returns>
        private bool IsFileInNeedTime(DateTime curTime, DateTime fileTime, FileInfo filePath,int changTime)
        {
            var cur = curTime - new DateTime(1970, 1, 1, 0, 0, 0);
            var file = fileTime - new DateTime(1970, 1, 1, 0, 0, 0);
            var fileChangeTime = file.TotalSeconds;
            //是否使用git 时间
            if (this.check_isGtTime.Checked)
            {
                fileChangeTime = this.GetFileGitCommandTime(filePath.FullName, filePath.DirectoryName);
            }
            // 修改时间内
            if (cur.TotalSeconds - changTime <= fileChangeTime)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 获取文件 git 提交的时间戳
        /// </summary>
        /// <param name="path"></param>
        /// <param name="workDirectory"></param>
        /// <returns></returns>
        private double GetFileGitCommandTime(string path, string workDirectory,int maxLogCount = 100)
        {
            string arguments = string.Format("log --pretty=format:\"%at\" -1 {0}", path);
            var info = new ProcessStartInfo("git", arguments)
            {
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                WorkingDirectory = workDirectory
            };
            string output = string.Empty;
            using (var process = new Process { StartInfo = info })
            {
                process.Start();
               int index = 0;
                StringBuilder sb = new StringBuilder();
                var streamReader = process.StandardOutput;
                while (!streamReader.EndOfStream && index < maxLogCount)
                {
                    sb.AppendLine(streamReader.ReadLine());
                    index++;
                }
                output = sb.ToString();
                var errorStream = process.StandardError;
                //error = errorStream.ReadToEnd();
                process.Close();
            }

            string[] lines = output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            double commitTime = 0;
            if (lines.Length > 0)
            {
                commitTime = double.Parse(lines[0]);
            }
            return commitTime;
        }

        private void SetCurPro(int cur, int all)
        {
            if (cur == 0 || all == 0)
            {
                this.prog.Value = 0;
            }
            else
            {
                this.prog.Value = (int)((cur * 1.0 / all) * 100);
            }
        }

        /// <summary>
        /// 普通目录下所有文件的最近变化(遍历底下多层目录)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_commonFile_Click(object sender, EventArgs e)
        {
            list_commonFileChange.Clear();

            string path = this.text_pathCommon.Text;

            this.GetFile(path, this.GetCurSetTime());



            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "audio(*.xlsx)|*.xlsx";
            saveFileDialog.ShowDialog();
            string savePath = saveFileDialog.FileName;


            ExcelTool excelTool = new ExcelTool(savePath);

            List<List<string>> heraders = new List<List<string>>();

            excelTool.CreatSheetData(heraders, list_commonFileChange,"近期修改");
            excelTool.SaveAllExcel();
        }

        /// <summary>
        /// 获取目录下修改文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="time"></param>
        private void GetFile(string path,int time)
        {
            var curDataTime = DateTime.Now.Date;
            // 遍历路径下json 
            // 对比name 如果一样
            // 修改里面 的bundleName 然后保存
            DirectoryInfo TheFolder = new DirectoryInfo(path);

            DirectoryInfo[] curAllNextFolder = TheFolder.GetDirectories();

            foreach (var NextFolder in curAllNextFolder)
            {

                DirectoryInfo[] directory = NextFolder.GetDirectories();
                FileInfo[] fileInfos = NextFolder.GetFiles();
                if (fileInfos.Length > 0)
                {
                    foreach (var file in fileInfos)
                    {
                        if (string.IsNullOrEmpty(file.FullName) == false && file.FullName.Contains(".meta") == false)
                        {
                            if (this.IsFileInNeedTime(curDataTime, file.LastWriteTime, file, time))
                            {
                                List<string> list = new List<string>();
                                list.Add(file.FullName);
                                string[] fullname = file.FullName.Split('\\');
                                string[] names = fullname[fullname.Length - 1].Split('.');
                                string fileName = names[0];   // 文件名称
                                list.Add(fileName);
                                list_commonFileChange.Add(list);
                            }
                        }
                    }
                }
                if (directory.Length > 0)
                {
                    foreach (var item in directory)
                    {
                        this.GetFile(item.FullName, time);
                    }
                }
            }

            FileInfo[] curfileInfos = TheFolder.GetFiles();
            if (curfileInfos.Length > 0)
            {
                foreach (var file in curfileInfos)
                {
                    if (string.IsNullOrEmpty(file.FullName) && file.FullName.Contains(".meta") == false)
                    {
                        if (this.IsFileInNeedTime(curDataTime, file.LastWriteTime, file, time))
                        {
                            List<string> list = new List<string>();
                            list.Add(file.FullName);
                            string[] fullname = file.FullName.Split('\\');
                            string[] names = fullname[fullname.Length - 1].Split('.');
                            string fileName = names[0];   // 文件名称
                            list.Add(fileName);
                            list_commonFileChange.Add(list);
                        }
                    }
                }
            }

        }

        /// <summary>
        /// Snapshot 修改导致变化的 bank大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_snapshot_Click(object sender, EventArgs e)
        {
            string path = this.text_pathSnapshot.Text;

            this.SetBankFileSize();
            this.SetEventToBank();
            this.SetGuidToName();
            this.GetChangeSnapShotList(path, this.GetCurSetTime());
            this.GetSnapShotToEventList();
        }

        /// <summary>
        /// 获取 SnapShot 目录下有变化的文件列表 （获取文件名）
        /// </summary>
        private void GetChangeSnapShotList(string path,int time)
        {
            this.list_snapshot.Clear();
            var files = this.ChangeFileInTime(path, time);
            int all = files.Count;
            int cur = 0;
            foreach (var file in files)
            {
                string[] temp = file.Split('\\');
                string[] names = temp[temp.Length - 1].Split('.');
                string fileName = names[0];  // 文件名 
                string sName = this.GetSnapshotName(file);
                List<string> listStr = new List<string>();
                listStr.Add(sName);
                listStr.Add(fileName);
                this.list_snapshot.Add(listStr);
                cur++;
                this.setProBar(cur, all);
            }
        }

        /// <summary>
        /// 获取 SnapShot 变化对应的 event 列表
        /// </summary>
        private void GetSnapShotToEventList()
        {
            string eventFilePath = this.text_pathEvent.Text;

            if (Directory.Exists(eventFilePath))
            {
                FileInfo[] fileInfos = this.GetFileList(eventFilePath);
                int all = fileInfos.Length;
                int cur = 0;
                foreach (var file in fileInfos)
                {
                    this.SetSetEventToSnapshot(file.FullName);
                    cur++;
                    this.setProBar(cur, all);
                }
            }

            // 取列表塞进表格
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "audio(*.xlsx)|*.xlsx";
            saveFileDialog.ShowDialog();
            string savePath = saveFileDialog.FileName;


            ExcelTool excelTool = new ExcelTool(savePath);
            //修改的 Snapshot 列表
            List<List<string>> h1 = new List<List<string>>();
            List<string> h1_1 = new List<string>();
            h1_1.Add("修改的Snapshot名称");
            h1_1.Add("修改的Snapshot文件");
            h1.Add(h1_1);
            excelTool.CreatSheetData(h1, list_snapshot, "修改的Snapshot");

            // 修改Snapshot 对应的bank文件
            foreach (var snapshotS in list_snapshot)
            {
                string sId = snapshotS[1];  // snapshot 的id
                string sName = snapshotS[0];
                //var sList = this.dic_snapShotChange[]
                if (this.dic_snapShotChange.ContainsKey(sId))
                {
                    List<List<string>> header = new List<List<string>>();
                    List<string> header_1 = new List<string>();
                    header_1.Add("修改的bank名称");
                    header_1.Add("修改的bank大小");
                    header.Add(header_1);
                    List<List<string>> listS = new List<List<string>>();

                    var eventList = this.dic_snapShotChange[sId];
                    List<string> tempB = new List<string>();
                    float allSize = 0;
                    foreach (var eName in eventList)
                    {
                        if (this.dicEventToBankId.ContainsKey(eName))
                        {
                            string bankId = this.dicEventToBankId[eName];
                            string bankName = this.dicBankIdToName[bankId];
                            if (tempB.Contains(bankName) == false)
                            {
                                tempB.Add(bankName);
                                
                                List<string> banks = new List<string>();
                                banks.Add(bankName);
                                int size = this.dicBankToSize[bankName];
                                banks.Add(size + "KB");
                                allSize += size;
                                listS.Add(banks);
                            }
                        }
                    }
                    List<string> over = new List<string>();
                    over.Add("修改总大小");
                    over.Add((allSize/1024)+ "M");
                    listS.Add(over);

                    excelTool.CreatSheetData(header, listS, sName);
                }
            }

            //修改涉及的event
            List<List<string>> outEvenName = new List<List<string>>();
            foreach (var snapshotS in this.dic_snapShotChange.Values)
            {
                foreach (var eId in snapshotS)
                {
                    if (this.dicGuidToName.ContainsKey(eId))
                    {
                        List<string> one = new List<string>();
                        one.Add(this.dicGuidToName[eId]);
                        outEvenName.Add(one);
                    }
                }
            }
            excelTool.CreatSheetData(new List<List<string>>(), outEvenName, "修改涉及对应的event");
            excelTool.SaveAllExcel();
        }

        /// <summary>
        /// 获取音效事件中对应的 Snapshot设置
        /// </summary>
        /// <param name="path"></param>
        private void SetSetEventToSnapshot(string path)
        {
            if (File.Exists(path))
            {

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);

                XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
                string[] temp = path.Split('\\');
                string[] files = temp[temp.Length - 1].Split('.');
                string keyname = files[0];  // 文件名 

                foreach (XmlElement element in topM)
                {
                    XmlNodeList xmlNodeList = element.ChildNodes;

                    // 获取节点名称
                    string nodeName = this.GetNodeName(element.Attributes);

                    // SnapshotModule 节点取  name 和 对应的 Snapshot
                    if (nodeName == "SnapshotModule")
                    {
                        // 取 event 指向的 Snapshotid 
                        foreach (XmlElement el in xmlNodeList)
                        {
                            XmlAttributeCollection elattr = el.Attributes;
                            XmlNode elNode = elattr.Item(0);
                            string elName = elNode.Value;

                            if (elName == "event")
                            {
                                // event 文件对应 snapshot 的snapshotid
                                string snapshotid = el.InnerText;
                                //this.dicEventToSnapshot[keyname] = snapshotid;
                                if (dic_snapShotChange.ContainsKey(snapshotid))
                                {
                                    dic_snapShotChange[snapshotid].Add(keyname);
                                }
                                else
                                {
                                    dic_snapShotChange[snapshotid] = new List<string>();
                                    dic_snapShotChange[snapshotid].Add(keyname);
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取修改 Snapshot 的名称
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private string GetSnapshotName(string path)
        {
            string sName = string.Empty;
            if (File.Exists(path))
            {

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);

                XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;
                string[] temp = path.Split('\\');
                string[] files = temp[temp.Length - 1].Split('.');
                string keyname = files[0];  // 文件名 

                foreach (XmlElement element in topM)
                {
                    XmlNodeList xmlNodeList = element.ChildNodes;

                    // 获取节点名称
                    string nodeName = this.GetNodeName(element.Attributes);

                    // SnapshotModule 节点取  name 和 对应的 Snapshot
                    if (nodeName == "Snapshot")
                    {
                        // 取 event 指向的 Snapshotid 
                        foreach (XmlElement el in xmlNodeList)
                        {
                            XmlAttributeCollection elattr = el.Attributes;
                            XmlNode elNode = elattr.Item(0);
                            string elName = elNode.Value;

                            if (elName == "name")
                            {
                                // event 文件对应 snapshot 的snapshotid
                                string snapshotName = el.InnerText;
                                sName = snapshotName;
                            }
                        }
                    }
                }
            }
            return sName;
        }
    }

}
